package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.EnvironmentSpray;
import com.hengxinguotong.zhihuichengjian.treeview.RecyclerAdapter;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @CopyRight 恒信国通信息科技
 * @Author WuLei
 * @Date 2019/3/16
 *
 * 喷淋设备列表
 */

public class EnvironmentSprayListAdapter extends RecyclerAdapter<EnvironmentSpray, EnvironmentSprayListAdapter.EnvironmentSprayListViewHolder> {
    private OnThresholdListener onThresholdListener;
    private OnAutoSprayListener onAutoSprayListener;
    private OnOpenSprayListener onOpenSprayListener;
    private OnHandleSprayListener onHandleSprayListener;

    private boolean canSelected = false;

    public EnvironmentSprayListAdapter(Context context, List<EnvironmentSpray> dataList) {
        super(context, dataList);
    }

    public void setOnOpenSprayListener(OnOpenSprayListener onOpenSprayListener) {
        this.onOpenSprayListener = onOpenSprayListener;
    }

    public void setOnHandleSprayListener(OnHandleSprayListener onHandleSprayListener) {
        this.onHandleSprayListener = onHandleSprayListener;
    }

    public void setOnThresholdListener(OnThresholdListener onThresholdListener) {
        this.onThresholdListener = onThresholdListener;
    }

    public void setOnAutoSprayListener(OnAutoSprayListener onAutoSprayListener) {
        this.onAutoSprayListener = onAutoSprayListener;
    }

    public void setCanSelected(boolean canSelected) {
        this.canSelected = canSelected;
        notifyDataSetChanged();
    }

    @Override
    public EnvironmentSprayListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item_environment_spray_list, null);
        return new EnvironmentSprayListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(EnvironmentSprayListViewHolder holder, int position) {
        holder.bindData(position);
    }

    public class EnvironmentSprayListViewHolder extends RecyclerAdapter.BaseViewHolder<EnvironmentSpray>{
        @BindView(R.id.spray_name_tv)
        HXTextView sprayNameTv;
        @BindView(R.id.threshold_settings)
        HXTextView thresholdSettings;
        @BindView(R.id.spray_status_switch)
        Switch sprayStatusSwitch;
        @BindView(R.id.spray_check_box)
        HXTextView sprayCheckBox;
        @BindView(R.id.handle_spray)
        HXTextView handleSpray;
        @BindView(R.id.auto_spray)
        HXTextView autoSpray;

        public EnvironmentSprayListViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void bindData(int position) {
            final EnvironmentSpray environmentSpray = dataList.get(position);
            sprayNameTv.setText(environmentSpray.getName() + "\t" + environmentSpray.getNumber());
            if(canSelected){
                sprayCheckBox.setVisibility(View.VISIBLE);
                handleSpray.setOnClickListener(null);
                autoSpray.setOnClickListener(null);
                thresholdSettings.setOnClickListener(null);
                sprayStatusSwitch.setOnCheckedChangeListener(null);
                sprayStatusSwitch.setEnabled(false);
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onClickListener.onClick(environmentSpray);
                    }
                });
            }else {
                sprayCheckBox.setVisibility(View.GONE);
                handleSpray.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onHandleSprayListener.onHandleSpray(environmentSpray);
                    }
                });
                autoSpray.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onAutoSprayListener.onAutoSpray(environmentSpray);
                    }
                });
                thresholdSettings.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onThresholdListener.onThreshold(environmentSpray);
                    }
                });
                sprayStatusSwitch.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onOpenSprayListener.onOpenSpray(environmentSpray);
                    }
                });
                sprayStatusSwitch.setEnabled(true);
                itemView.setOnClickListener(null);
            }
            if(environmentSpray.isChecked()){
                sprayCheckBox.setEnabled(true);
            }else {
                sprayCheckBox.setEnabled(false);
            }
            if(environmentSpray.getSprayStatus() == 1){//喷淋状态:0手动,1自动
                autoSpray.setEnabled(false);
                sprayStatusSwitch.setVisibility(View.GONE);
                handleSpray.setEnabled(true);
                thresholdSettings.setVisibility(View.VISIBLE);
            }else {
                handleSpray.setEnabled(false);
                thresholdSettings.setVisibility(View.GONE);
                autoSpray.setEnabled(true);
                sprayStatusSwitch.setVisibility(View.VISIBLE);
            }
            if(Utils.isEmpty(environmentSpray.getStatus()) || environmentSpray.getStatus().equals("0000")){//	喷淋开关状态 空字符串 或 0000 关； 1111 开
                sprayStatusSwitch.setChecked(false);
            }else {
                sprayStatusSwitch.setChecked(true);
            }

        }
    }

    public interface OnHandleSprayListener<K>{
        void onHandleSpray(K k);
    }

    public interface OnAutoSprayListener<K>{
        void onAutoSpray(K k);
    }

    public interface OnThresholdListener<K>{
        void onThreshold(K k);
    }

    public interface OnOpenSprayListener<K>{
        void onOpenSpray(K k);
    }
}
